package com.lwq.project.activemq.impl;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.lwq.project.activemq.ProducerService;

/**
* @author 作者 linweiqiang
* @version 创建时间：2017年10月20日 上午10:28:41
* 类说明
*/
@Service
public class ProducerServiceImpl implements ProducerService {
	
	private Logger logger = Logger.getLogger(ProducerServiceImpl.class);
	
	@Resource(name="jmsQueueTemplate")
	private JmsTemplate jmsQueueTemplate ;
	@Resource(name="jmsTopicTemplate")
	private JmsTemplate jmsTopicTemplate ;
	
	public void sendMessageQueue(final String msg) {
		logger.error("向默认消息队列Queue发送消息------->" + msg);
		jmsQueueTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(msg);
			}
		});
	}

	public void sendMessageQueue(String name, final String msg) {
		if(!StringUtils.isBlank(name)){
			Destination destination = ActiveMQQueue.createDestination(name, ActiveMQQueue.TOPIC_TYPE);
			logger.error("向默认消息队列Queue" + destination.toString() + "发送消息------>" + msg);
			jmsQueueTemplate.send(destination,new MessageCreator() {
				public Message createMessage(Session session) throws JMSException {
					return session.createTextMessage(msg);
				}
			});
		}
	}
	public void sendMessageTopic(final String msg) {
		logger.error("向默认消息队列Topic发送消息------->" + msg);
		jmsTopicTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(msg);
			}
		});
	}
	
	public void sendMessageTopic(String name, final String msg) {
		if(!StringUtils.isBlank(name)){
			Destination destination = ActiveMQTopic.createDestination(name, ActiveMQTopic.TOPIC_TYPE);
			logger.error("向默认消息队列Topic" + destination.toString() + "发送消息------>" + msg);
			jmsTopicTemplate.send(destination,new MessageCreator() {
				public Message createMessage(Session session) throws JMSException {
					return session.createTextMessage(msg);
				}
			});
		}
	}

}
